﻿using System;
using System.Data;
using System.Collections.Generic;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

namespace EHSubmit.Util
{
    /// <summary>
    /// 操作JSON函数
    /// </summary>
    public class JsonHelper
    {
        

        /// <summary>
        /// 获得对象经Json序列化后的字符串(日期按JavaScript日期编码)
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="o">需要序列化的对象</param>
        /// <returns>JSON字符串</returns>
        public static string Serialize(Object o)
        {
            try
            {
                JsonSerializerSettings jsSettings = new JsonSerializerSettings();
                jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

                jsSettings.Converters.Add(new JavaScriptDateTimeConverter());
                return JsonConvert.SerializeObject(o, Formatting.None, jsSettings);
                //return JsonConvert.SerializeObject(o, new JavaScriptDateTimeConverter());
            }
            catch (Exception e)
            {
                LogHelper.Log(e.Message, e);
                return string.Empty;
            }
        }

        /// <summary>
        /// 将Json字串反序列化为对象(日期按JavaScript日期反编码)
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="json">json字串</param>
        /// <returns>对象</returns>
        public static T Deserialize<T>(string json)
        {
            T o = JsonConvert.DeserializeObject<T>(json, new JavaScriptDateTimeConverter());
            
            return o;
        }

        public static object GetObject(string json)
        {
            return JsonConvert.DeserializeObject(json);

        }

    }
}